<?php

namespace app\task\cron\agent;

use app\model\Agent;
use think\Exception;
use yunwuxin\cron\Task;

class AgentBalanceSecurityWarning extends Task
{
    public function configure()
    {
        // 分 时 日 月 周
        $this->expression = '* * * * *';
    }

    public function handle()
    {

        echo '代理商余额安全警告开始--' . date('Y-m-d H:i:s') . PHP_EOL;
        $lockKey = 'AgentBalanceSecurityWarning';
        $lockVal = cache($lockKey);

        if ($lockVal) {
            echo '还在处理呢' . PHP_EOL;

            return true;
        }
        cache($lockKey, 1, 120);

        Agent::query()
            ->where('security_tag','<>','')
            ->where('status',Agent::STATUS_ACTIVE)
            ->select()->each(function (Agent $agent){
                if (!password_verify($agent->total_profit.$agent->profit_balance,$agent->security_tag)){

                    $url = 'https://oapi.dingtalk.com/robot/send?access_token=d7bb23b1e0b95b92dd55e7f75d30230774ffa978e0d062ff9202cbe37cf38c9a';
                    $header = ['Content-Type: application/json; charset=utf-8'];
                    curlPost($url,json_encode([
                        'msgtype' => 'text',
                        'text' => [
                            'content' => '警告:'.$agent->company_name.'('.$agent->username.')'.'agent_id:'.$agent->id.'可能出现篡改数据，排查问题'
                        ],
                    ],JSON_UNESCAPED_UNICODE),$header);
                }
            });

        cache($lockKey, null);
    }
}